package com.cisdi.blog.service.impl;

import com.cisdi.blog.dao.RecordRepository;
import com.cisdi.blog.entity.Record;
import com.cisdi.blog.service.RecordService;
import com.cisdi.blog.util.BeanUtilsExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author pengyangyan
 */
@Service
public class RecordServiceImpl implements RecordService {

    @Autowired
    RecordRepository recordRepository;

    @Override
    public Page<Record> pageRecords(Pageable pageable) {
        return recordRepository.getAll(pageable);
    }

    @Override
    public Record getRecord(Long id) {
        return recordRepository.getOne(id);
    }

    @Override
    public Record addRecord(Record record) {
        record.setCreateTime(new Date());
        record.setUpdateTime(new Date());
        return recordRepository.save(record);
    }

    @Override
    public void deleteRecord(Long id) {
        recordRepository.deleteById(id);
    }

    @Override
    public Record updateRecord(Record record) {
        Record one = recordRepository.getOne(record.getId());
        BeanUtilsExt.copyBeanNotCopyNullValue(record,one);
        one.setUpdateTime(new Date());
        return recordRepository.save(one);
    }
}
